﻿using Business.Base.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business.ScrewPump.Perf
{
    public class StandLevelJudger
    {
        #region 标准判断


        private static Double getPressureLevelValue(Double pressure, PresureFlowLevel level)
        {
            return 1.0;
        }

        /// <summary>
        /// 根据流量值获取lewel级别下该值的范围
        /// </summary>
        /// <param name="flow">输入的流量</param>
        /// <param name="level">输入级别</param>
        /// <returns></returns>
        private static Double[] getFlowLevelValue(Double flow, PresureFlowLevel presureFlowLevel)
        {
            int level = (int)presureFlowLevel;
            if ((0.1 - flow) > Double.Epsilon)
            {
                if (level == 1)
                {
                    return new double[] { 10.0, 10.0 };
                }
                else if (level == 2)
                {
                    return new double[] { 20.0, 10 };
                }
            }
            else if ((flow - 0.1) > Double.Epsilon && (10 - flow) > Double.Epsilon)
            {
                if (level == 1)
                {
                    return new double[] { 5, 5 };
                }
                else if (level == 2)
                {
                    return new double[] { 10, 10 };
                }
            }
            else if ((flow - 10) > Double.Epsilon)
            {
                if (level == 1)
                {
                    return new double[] { 5, 5 };
                }
                else if (level == 2)
                {
                    return new double[] { 10, 5 };
                }
            }
            return null;
        }

        /// <summary>
        /// 根据功率值获取level下该值的范围
        /// </summary>
        /// <param name="power">输入的功率</param>
        /// <param name="level">输入的级别</param>
        /// <returns></returns>
        private static Double[] getPowerLevelValue(Double power, PresureFlowLevel presureFlowLevel)
        {
            String level = ((int)presureFlowLevel).ToString();
            if ((5 - power) > Double.Epsilon)
            {
                return new double[] { 25.0, 0 };
            }
            else if ((power - 5) > Double.Epsilon && (10 - power) > Double.Epsilon)
            {
                if (level.Equals("1"))
                {
                    return new double[] { 15.0, 0 };
                }
                else if (level.Equals("2"))
                {
                    return new double[] { 20.0, 0 };
                }
            }
            else if ((power - 10) > Double.Epsilon && (50 - power) > Double.Epsilon)
            {
                if (level.Equals("1"))
                {
                    return new double[] { 10.0, 0 };
                }
                else if (level.Equals("2"))
                {
                    return new double[] { 15.0, 0 };
                }
            }
            else if ((power - 50) > Double.Epsilon)
            {
                if (level.Equals("1"))
                {
                    return new double[] { 5.0, 0 };
                }
                else if (level.Equals("2"))
                {
                    return new double[] { 10.0, 0 };
                }
            }
            return null;
        }

        /// <summary>
        /// 获取指定效率在对应的level下的取值范围
        /// </summary>
        /// <param name="rateEff">效率</param>
        /// <param name="level">取值范围，是一个指定的数值</param>
        /// <returns></returns>
        private static double[] getEffLevelValue(double rateEff, PresureFlowLevel level)
        {
            return new double[] { 0, 5.0 };
        }

        /// <summary>
        /// 流量标准判断
        /// </summary>
        /// <param name="head">计算流量</param>
        /// <param name="ratedHead">额定流量</param>
        /// <param name="level">判断等级</param>
        /// <returns></returns>
        public static Boolean JudgeFlowLevel(Double flow, Double ratedFlow, PresureFlowLevel level)
        {
            Double[] rate = getFlowLevelValue(flow, level);
            if (((ratedFlow * (100 + rate[0]) / 100.0) - flow) > Double.Epsilon && (flow - (ratedFlow * (100 - rate[1]) / 100.0f) > Double.Epsilon))
            {
                return true;
            }
            return false;

        }

        /// <summary>
        /// 功率标准判断
        /// </summary>
        /// <param name="power">功率</param>
        /// <param name="ratedPower">额定功率</param>
        /// <param name="level">等级</param>
        /// <returns></returns>
        public static Boolean JudgePowerLevel(Double power, Double ratedPower, PresureFlowLevel level)
        {
            double[] rate = getPowerLevelValue(power, level);
            if (((ratedPower * (100 + rate[0]) / 100.0f) - power) > Double.Epsilon && (power - (ratedPower * (100 - rate[1]) / 100.0f) > Double.Epsilon))
            {
                return true;
            }
            return false;
        }

        public static Boolean JudgeEffLevel(Double eff, Double ratedEff, PresureFlowLevel level)
        {
            double[] rate = getEffLevelValue(eff, level);
            if (((ratedEff * (100 + rate[0]) / 100.0f) - eff) > Double.Epsilon && (eff - (ratedEff * (100 - rate[1]) / 100.0f) > Double.Epsilon))
            {
                return true;
            }
            return false;
        }


       

        /// <summary>
        /// 根据额定流量，计算出工况流量在level上的允许范围，支持上下界值。
        /// </summary>
        /// <param name="ratedFlow">根据额定压力代入工况下的压力流量曲线计算出来的流量</param>
        /// <param name="level">预期的level</param>
        /// <returns>返回工况流量在level上的允许范围，支持上下界值。</returns>
        public static double[] GetFlowRange(double ratedFlow, PresureFlowLevel level)
        {
            double[] flowRate = getFlowLevelValue(ratedFlow, level);
            return new double[]
            {
                    ratedFlow*(1+flowRate[0]/100.0),ratedFlow*(1-flowRate[1]/100.0)
             };
        }

        public static double[] GetPowerRange(double ratedPower, PresureFlowLevel level)
        {
            double[] powerRate = getPowerLevelValue(ratedPower / 1000.0, level);
            return new double[]
            {
               ratedPower * (1 + powerRate[0] / 100.0),ratedPower * (1 - powerRate[1] / 100.0)
            };
        }

        public static double[] GetEffRange(double ratedEff, PresureFlowLevel level)
        {
            double[] effRate = getEffLevelValue(ratedEff, level);
            return new double[]
                { ratedEff * (1 + effRate[0] / 100.0),ratedEff * (1 - effRate[1] / 100.0) };
        }

     #endregion

    }
}
